package com.seednia.module.core.post.dto;

import org.apache.commons.lang.StringUtils;

import com.seednia.foundation.framework.dto.AbstractRecord;
import com.seednia.foundation.framework.dto.ICriteria;
import com.seednia.module.core.search.util.AbstractQueryBuilder;

public class PostQueryBuilder extends AbstractQueryBuilder {

	@Override
	public String buildQueryString(ICriteria record) {
		PostDto dto = (PostDto)record;
		StringBuilder retval = new StringBuilder();
		// OR-ed 
		if ( StringUtils.isNotBlank(dto.getBody()) ) {
			if (retval.length() > 1) retval.append(" OR");
			retval.append(" body:(").append(AbstractQueryBuilder.escape(dto.getBody())).append(")");
		}
		if ( StringUtils.isNotBlank(dto.getSubject()) ) {
			if (retval.length() > 1) retval.append(" OR");
			retval.append(" subject:(").append(AbstractQueryBuilder.escape(dto.getSubject())).append(")");
		}
		
		// Enclose in parenthesis so we can AND them 
		if (retval.length() > 1) {
			retval.insert(0, "(").append(")");
		}
		// TODO: add author
		if (retval.length() > 1) retval.append(" AND");
		retval.append(" privacyLevel:[0 TO ").append(dto.getPrivacyLevel()).append("]");
				
		return retval.toString();
	}

}
